package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectAllDeptDTO;
import com.itheima.tlias84.entity.Dept;
import com.itheima.tlias84.mapper.DeptMapper;
import com.itheima.tlias84.po.InsertDeptParam;
import com.itheima.tlias84.po.UpdateDeptParam;
import com.itheima.tlias84.service.DeptService;
import com.itheima.tlias84.utils.ResultUtil;
//import jakarta.servlet.http.HttpServletRequest;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//import java.io.BufferedReader;
//import java.io.IOException;
//import java.time.format.DateTimeFormatter;
//import java.util.HashMap;
import java.util.List;
//import java.util.Map;
//import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/depts")
/**
 * @RequestMapping Target - > 用在类上和方法上
 * 用在类上->提取一个controller中的重复路径
 * 用在方法上呢->定义该接口的请求方法的 被对应的GetMapping .. PostMapping替代了
 */
public class DeptController {
    @Autowired
    DeptMapper deptMapper;

    //    private final DeptService deptService;
//
//    @Autowired
//    public DeptController(DeptService deptService) {
//        this.deptService = deptService;
//    }

    /**
     * 属性注入优先使用@Resource
     */
    @Resource
    DeptService service;


    @GetMapping
    public ResultUtil selectAll() {
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//        List<SelectAllDeptDTO> selectAllDeptDTOS1 = deptMapper.selectAll();
//        HashMap<String,Object> map = new HashMap<>();
//        List<SelectAllDeptDTO> list = depts.stream().map((dept) -> {
//            SelectAllDeptDTO selectAllDeptDTO = new SelectAllDeptDTO();
//            selectAllDeptDTO.setId(dept.getId());
//            selectAllDeptDTO.setName(dept.getName());
//            String format = dept.getUpdateTime().format(formatter);
//            selectAllDeptDTO.setUpdateTime(format);
//            return selectAllDeptDTO;
//        }).toList();
//        map.put("code",1);
//        map.put("msg","成功");
//        map.put("data",selectAllDeptDTOS);
        List<SelectAllDeptDTO> selectAllDeptDTOS = service.selectAll();
        return ResultUtil.success(selectAllDeptDTOS);
    }


    /**
     * get/delete 请求参数一般拼接url后面
     * post/put 请求参数一般放在请求体里面，并且以json格式进行传输
     *
     * @param param
     * @return
     * @RequestBody 作用一就是从http请求体中读取传输的数据
     * 作用二就是把JSON字符串自动变成对象
     */
    @PostMapping
    public ResultUtil insertDept(@RequestBody InsertDeptParam param) {
//        try {
//            BufferedReader reader = request.getReader();
//            log.info("读取请求体中的数据{}",reader);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        log.info("数据{}",name);
//        String[] split = name.split(":");
//        String s = split[1];
//        log.info("数据{}",param.getName());
//        Dept dept = new Dept();
//        dept.setName(param.getName());
//        dept.setCreateTime(LocalDateTime.now());
//        dept.setUpdateTime(LocalDateTime.now());
//        int flag = deptMapper.insertDept(dept);
//        HashMap<String,Object> map = new HashMap<>();
//        map.put("code",flag);
//        map.put("msg","成功");
//        map.put("data",null);
        log.info("数据{}", param.getName());
        int i = service.insertDept(param);
        return i==0?ResultUtil.fail("新增失败") :ResultUtil.success("新增成功");
    }


    /**
     * @requestParam注解的作用：
     *              1.根据指定的K获取HTTP URL中的值  value/name
     *              2.可以指定是否必传的参数，如果制定了必传，前端如果没传相应参数 报错 400 required
     *              3.给参数指定默认值  defaultValue
     * @param id
     * @return
     */
    @DeleteMapping
    public ResultUtil deleteById(@RequestParam("id") Integer id){
        int i = service.deleteById(id);
        return i==0?ResultUtil.fail() :ResultUtil.success();
    }

    /**
     * @PathVariable 注解作用
     * 获取URI以/分隔的路径变量
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultUtil selectById(@PathVariable Integer id){
        UpdateDeptParam updateDeptParam = service.selectById(id);
        return ResultUtil.success(updateDeptParam);
    }
    @PutMapping
    public ResultUtil updateById(@RequestBody Dept dept){
        return service.updateById(dept);
    }

    @PostMapping("/1")
    public ResultUtil insertDeptA(@RequestBody InsertDeptParam param) {
        int i = deptMapper.insertDeptA(param);
        return i==0?ResultUtil.fail("新增失败") :ResultUtil.success("新增成功");
    }

    @DeleteMapping("batchDelete")
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids){
        log.info("接收到的id={}",ids);
        int i = service.batchDelete(ids);
        return ResultUtil.success();
    }
}
